import re

import requests

from codes.commons.configs import easiest_common_headers as common_headers


def get_data(page):
    url = 'http://match.yuanrenxue.com/match/13'
    response = requests.get(url=url, headers=common_headers)
    cookie = ''.join(re.findall(r"'(.*?)'", response.text))[:-7]
    session_id = response.headers['Set-Cookie'].split(';')[0]

    url_api = f'http://match.yuanrenxue.com/api/match/13?page={page}'
    headers = {
        'User-Agent': 'yuanrenxue.project',
        'Cookie': session_id + ';' + cookie
    }
    res = requests.get(url=url_api, headers=headers)
    print(f'获取第【{page}】页数据：{res.text}')
    return res.json()['data']


if __name__ == '__main__':
    nums_sum = []
    for i in range(1, 6):
        data = [i['value'] for i in get_data(i)]
        nums_sum.extend(data)
    print(f'总计:{sum(nums_sum)}')
